package com.gidgabs.common.utils;


import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.gidgabs.common.exception.RRException;
import com.gidgabs.config.GidgabsContants;
import com.gidgabs.dto.VideoDto;
import org.apache.commons.lang.StringUtils;

import java.rmi.ServerException;
import java.util.List;

/**
 * 以下Java示例代码演示了如何在服务端上传文件至视频点播。
 * 目前支持两种方式上传：
 * 1.上传本地文件，使用分片上传，并支持断点续传，参见testUploadVideo函数。
 * 1.1 当断点续传关闭时，最大支持上传任务执行时间为3000秒，具体可上传文件大小与您的网络带宽及磁盘读写能力有关。
 * 1.2 当断点续传开启时，最大支持48.8TB的单个文件，注意，断点续传开启后，上传任务执行过程中，同时会将当前上传位置写入本地磁盘文件，影响您上传文件的速度，请您根据文件大小选择是否开启
 * 2.上传网络流，可指定文件URL进行上传，不支持断点续传，最大支持5GB的单个文件。参见testUploadURLStream函数。
 * 3.上传文件流，可指定本地文件进行上传，不支持断点续传，最大支持5GB的单个文件。参见testUploadFileStream函数。
 * 请替换示例中的必选参数，示例中的可选参数如果您不需要设置，请将其删除，以免设置无效参数值与您的预期不符。
 */
public class UploadVideoUtils {
    private static DefaultAcsClient client;
    /**
     * 初始化客户端
     *
     * @return
     */
    public synchronized static DefaultAcsClient initVodClient() {
        //点播服务所在的Region，国内请填cn-shanghai，不要填写别的区域
        if (client == null) {
            String regionId = "cn-shanghai";
            DefaultProfile profile = DefaultProfile.getProfile(regionId, GidgabsContants.accessKeyId, GidgabsContants.accessKeySecret);
            client = new DefaultAcsClient(profile);
            return client;
        } else {
            return client;
        }

    }

    /*获取上传地址和凭证函数*/
    public static VideoDto createUploadVideo(String title, String description, String fileName, String coverURL) {
        if (StringUtils.isBlank(description)) {
            description = "gidgabs视频";
        }
        if (StringUtils.isBlank(fileName)) {
            fileName = "gidgabs视频";
        }
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(title);
        request.setDescription(description);
        request.setFileName(fileName);
        request.setTags("gidgabs");
        request.setCoverURL(coverURL);
        try {
            CreateUploadVideoResponse acsResponse = initVodClient().getAcsResponse(request);
            VideoDto videoDto = new VideoDto();
            videoDto.setUploadAddress(acsResponse.getUploadAddress());
            videoDto.setUploadAuth(acsResponse.getUploadAuth());
            videoDto.setVideoId(acsResponse.getVideoId());
            return videoDto;
        } catch (com.aliyuncs.exceptions.ClientException e) {
            throw new RRException("获取上传凭证失败", e);
        }
    }
    /*以下为调用示例*/
    /*public static void main(String[] argv) {
        DefaultAcsClient client = initVodClient();
        CreateUploadVideoResponse response = new CreateUploadVideoResponse();
        try {
            VideoDto uploadVideo = createUploadVideo("test", "test", "test.mp4", "http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png");
            System.out.print("VideoId = " + response.getVideoId() + "\n");
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }*/


    /*刷新视频上传凭证函数*/
    public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient client) throws Exception {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId("视频ID");
        return client.getAcsResponse(request);
    }
//    /*以下为调用示例*/
//    public static void main(String[] argv) {
//        DefaultAcsClient client = initVodClient();
//        RefreshUploadVideoResponse response = new RefreshUploadVideoResponse();
//        try {
//            response = refreshUploadVideo(client);
//            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
//            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
//        } catch (Exception e) {
//            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
//        }
//        System.out.print("RequestId = " + response.getRequestId() + "\n");
//    }


//    /*以下为调用示例*/
//    public static void main(String[] argv) {
//        DefaultAcsClient client = initVodClient();
//        CreateUploadImageResponse response = new CreateUploadImageResponse();
//        try {
//            response = createUploadImage(client);
//            System.out.print("ImageId = " + response.getImageId() + "\n");
//            System.out.print("ImageURL = " + response.getImageURL() + "\n");
//            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
//            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
//        } catch (Exception e) {
//            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
//        }
//        System.out.print("RequestId = " + response.getRequestId() + "\n");
//    }

    //获取播放凭证
    public static VideoDto getVideoPlayAuth(String videoId) {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        GetVideoPlayAuthResponse response = null;
        try {
            response = initVodClient().getAcsResponse(request);
            VideoDto videoDto = new VideoDto();
            videoDto.setPlayAuth(response.getPlayAuth());
            videoDto.setVideoId(videoId);
            return videoDto;
        } catch (Exception e) {
            throw new RRException("获取播放凭证", e);
        }
    }
    //    /*以下为调用示例*/
//    public static void main(String[] argv) {
//        DefaultAcsClient client = new DefaultAcsClient(profile);
//        getVideoPlayAuth(client);
//    }


    /*获取播放地址函数*/
    /*public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId("视频ID");
        return client.getAcsResponse(request);
    }*/
//    /*以下为调用示例*/
//    public static void main(String[] argv) {
//        DefaultAcsClient client = initVodClient();
//        GetPlayInfoResponse response = new GetPlayInfoResponse();
//        try {
//            response = getPlayInfo(client);
//            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
//            //播放地址
//            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
//                System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
//            }
//            //Base信息
//            System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
//        } catch (Exception e) {
//            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
//        }
//        System.out.print("RequestId = " + response.getRequestId() + "\n");
//    }

}